from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from app.ser import ClsSeria,StudentSeria
from app.models import ClsModel,StudentModel
# Create your views here.
class ClsViews(APIView):
    def get(self,request,name):
        # name=request.query_params.get('name')
        c1=ClsModel.objects.get(name__exact=name)
        stu=c1.studentmodel_set.all()
        stuser=StudentSeria(instance=stu,many=True)
        print(stuser.data)
        return Response({'message':'请求成功了','data':stuser.data})
    def post(self,request):
        clsser=ClsSeria(data=request.data)
        if clsser.is_valid():
            clsser.save()
            return Response({"message": "添加成功", "code": "100000"})
        else:
            print('数据校验错误:', clsser.errors)
            return Response({"message": "添加失败", "code": "100001"})

class StudentViews(APIView):
    def get(self,request):
        stu=StudentModel.objects.all()
        stuser = StudentSeria(instance=stu,many=True)
        print(stuser.data)
        return Response({'message':'请求成功了','data':stuser.data})

    def post(self,request):
        stuser=StudentSeria(data=request.data)
        if stuser.is_valid():
            stuser.save()
            return Response({"message": "添加成功", "code": "100000"})
        else:
            print('数据校验错误:',stuser.errors)
            return Response({"message": "添加失败", "code": "100001"})

